<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div class="left">
        <img src="../assets/imgs/shop.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <div class="right">
        <span>欢迎您：{{ userInfo.username }}</span>
        <el-button type="danger" size="mini" @click="logout">退出</el-button>
      </div>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '60px' : '200px'">
		<!-- 折叠 -->
		<div class="toggle-button" @click="isCollapse = !isCollapse">|||</div>
        <el-menu unique-opened router :default-active="$route.path" :collapse="isCollapse" :collapse-transition="false">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id">
				<i class="el-icon-menu"></i>
				{{ subItem.authName }}
			</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
		<router-view></router-view>
	  </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
	data() {
		return {
			userInfo: {},
			menuList:[],
			// 图标样式对象
			iconsObj:{
				'201':'iconfont icon-shouye',
				'125':'iconfont icon-users',
				'103':'iconfont icon-tijikongjian',
				'101':'iconfont icon-danju',
				'102':'iconfont icon-baobiao',
			},
			isCollapse:false,
		}
	},
	created() {
		this.userInfo = JSON.parse(sessionStorage.getItem('userInfo'))

		this.getMenuList()
	},
	methods: {
		logout() {
			this.$confirm('确定要退出吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					sessionStorage.clear()
					this.$router.push('/login')
				})
				.catch(() => {})
		},
		// 获取左侧菜单
		async getMenuList() {
			const { data: res } = await this.$http.get('menus')
			this.menuList = res.data
		},
	},
}
</script>

<style lang="less" scoped>
.el-container {
	height: 100%;
}
.el-header {
	background: url('../assets/imgs/header_bg.gif') repeat-x center;
	height: 50px !important;
	display: flex;
	justify-content: space-between;
	align-items: center;
	color: white;

	.left {
		display: flex;
		align-items: center;
		img {
			width: 40px;
			margin-right: 20px;
		}
		span {
			font-size: 20px;
		}
	}
	.right {
		span {
			margin-right: 20px;
		}
	}
}
.iconfont{
	margin-right: 10px;
}
.el-main{
	background: #eaedf1;
}
.toggle-button{
	background: #60779d;
	color: white;
	font-size: 10px;
	line-height: 24px;
	text-align: center;
	letter-spacing: 3px;
	cursor: pointer;
}
</style>